/*
 * @Copyright: Copyright (c) 2019
 * @Author: 一枚小工
 * @Version: 1.0
 * @Date: 2019-11-01 18:12:54
 */
import { _decorator, Component, Node, systemEvent, SystemEvent, SphereColliderComponent, RigidBodyComponent, Vec3, math, EventTouch,Touch,Vec2 } from "cc";
import { Direct } from "./Direct";
const { ccclass, property } = _decorator;

@ccclass("Ball")
export class Ball extends Component {
    @property(Direct)
    private diect: Direct = null;

    private ballRigid: RigidBodyComponent = null; // 球刚体
    private force: number = 5;
    private angle: number = Math.PI / 4;

    /* class member could be defined like this */
    // dummy = '';

    /* use `property` decorator if your want the member to be serializable */
    // @property
    // serializableDummy = 0;

    onLoad(): void{
        this.ballRigid = this.node.getComponent(RigidBodyComponent);
        systemEvent.on(SystemEvent.EventType.TOUCH_START, this.onTouchStart, this);
        systemEvent.on(SystemEvent.EventType.TOUCH_MOVE, this.onTouchMove, this);
        systemEvent.on(SystemEvent.EventType.TOUCH_END, this.onTouchEnd, this)
    }

    onTouchStart(touch:Touch, event: EventTouch): void{
        
    }

    onTouchMove(touch:Touch, event: EventTouch): void{
        let start: Vec2 = touch.getStartLocation();
        let move: Vec2 = touch.getLocation();
        let delta = move.subtract(start);
        cc.log(delta);
        let tan: number = delta.y / delta.x;
        let angle: number = Math.atan(-tan);
        cc.log(angle);
        angle = -(180 / Math.PI * angle);
        cc.log(angle);
        angle += 90;
        cc.log('d', angle);
        // this.diect.setDirect(angle);
    }

    onTouchEnd(touch:Touch, event: EventTouch):void{
        cc.log('end');
        let v: Vec3 = cc.v3();
        v.x = 0;
        v.y = this.force * Math.sin(this.angle);
        v.z = -this.force * Math.cos(this.angle);
        this.ballRigid.setLinearVelocity(v);
    }

    start () {
        // Your initialization goes here.
    }

    // update (deltaTime: number) {
    //     // Your update function goes here.
    // }
}
